package cn.blnp.net.flowable.boot.custom.element;

import cn.blnp.net.flowable.boot.utils.SpringContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.stereotype.Component;

/**
 * <h3></h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/5 17:39
 */
@Slf4j
@Component
public class MyTaskListener extends ListenerUtil implements TaskListener {

    @Override
    public void notify(DelegateTask delegateTask) {
        RepositoryService repositoryService = SpringContextUtils.getBean(RepositoryService.class);
        //获取流程BpmnModel
        BpmnModel bpmnModel = repositoryService.getBpmnModel(delegateTask.getProcessDefinitionId());
        //查询用户任务的FlowElement
        FlowElement flowElement = bpmnModel
                .getFlowElement(delegateTask.getTaskDefinitionKey());
        //获取用户任务自定义属性enableToDoNotice的值
        String enableToDoNotice = super
                .getExtensionAttributeValue(flowElement , "enableToDoNotice");
        if (StringUtils.isNotBlank(enableToDoNotice) && Boolean.parseBoolean(enableToDoNotice)) {
            //查询用户任务自定义属性noticeContent配置的内容
            String noticeContent = super
                    .getExtensionElementValue(flowElement, "noticeContent");
            //计算表达式获取最终的值
            String noticeContentValue = super.getExpressValue(noticeContent, delegateTask);
            log.info("发送待办通知：{}", noticeContentValue);
        }
    }
}
